﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EvidenceLib.AF;
using EvidenceLib.Case;
using EvidenceService.Core;
using NHibernate;

namespace EvidenceService.AF
{
    public class AttachmentService : BasicService<Attachment>
    {
        public AttachmentService(ISession session)
            : base(session)
        {

        }

        public void SaveOrUpdate(int CaseId,int billType, int billId, string name,string ext,string url,string description, long size, int deptId, int userId)
        {

            /*                
             * string sql = string.Format(" if exists (select 1 from Ev_Attachment where billType ={0} and billId={1} and Name ='{2}')", billType, billId, name);
                sql += Environment.NewLine;
                sql += string.Format("  update Ev_Attachment set Ext='{0}',Url='{1}',Description='{2}',Size={3},DepartmentId={4},SubmitterId={5},SubmitTime=getDate() where billType={6} and billId={7} and name='{8}'", ext, url, description,size, deptId, userId, billType, billId, name);
                sql += Environment.NewLine;
                sql += "else";
                sql += Environment.NewLine;
                sql += string.Format("  insert into Ev_Attachment (BillType,BillId,Name,Ext,Url,Description,Size,DepartmentId,SubmitterId,SubmitTime) values({0},{1},'{2}','{3}','{4}','{5}',{6},{7},{8},getDate())", billType, billId, name, ext, url, description,size, deptId, userId);

             * 
             * 
            */
            //Description=@des,
            try
            {
                string sql = "if exists (select 1 from Ev_Attachment where billType =@billType and billId=@billId and Name =@name)";
                sql += Environment.NewLine;
                sql += "  update Ev_Attachment set Ext=@ext,Url=@url,Size=@size,DepartmentId=@deptId,SubmitterId=@submitterId,SubmitTime=getDate() where billType=@billType and billId=@billId and name=@name";
                sql += Environment.NewLine;
                sql += "else";
                sql += Environment.NewLine;
                sql += "  insert into Ev_Attachment (CaseId,BillType,BillId,Name,Ext,Url,Description,Size,DepartmentId,SubmitterId,SubmitTime) values(@CaseId,@billType,@billId,@name,@ext,@url,'',@size,@deptId,@submitterId,getDate())";

                SqlCommand cmd = mSession.Connection.CreateCommand() as SqlCommand;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;

                SqlParameter[] pars = new SqlParameter[9];

                pars[0] = cmd.CreateParameter();
                pars[0].ParameterName = "@billType";
                pars[0].DbType = DbType.Int32;
                pars[0].Value = billType;

                pars[1] = cmd.CreateParameter();
                pars[1].ParameterName = "@billId";
                pars[1].DbType = DbType.Int32;
                pars[1].Value = billId;

                pars[2] = cmd.CreateParameter();
                pars[2].ParameterName = "@name";
                pars[2].DbType = DbType.String;
                pars[2].Size = 200;
                pars[2].Value = name;

                pars[3] = cmd.CreateParameter();
                pars[3].ParameterName = "@ext";
                pars[3].DbType = DbType.String;
                pars[3].Size = 50;
                pars[3].Value = ext;

                pars[4] = cmd.CreateParameter();
                pars[4].ParameterName = "@url";
                pars[4].DbType = DbType.String;
                pars[4].Size = 200;
                pars[4].Value = url;

                //pars[5] = cmd.CreateParameter();
                //pars[5].ParameterName = "@des";
                //pars[5].DbType = DbType.String;
                //pars[5].Size = 500;
                //pars[5].Value = description;

                pars[5] = cmd.CreateParameter();
                pars[5].ParameterName = "@size";
                pars[5].DbType = DbType.Int32;
                pars[5].Value = size;

                pars[6] = cmd.CreateParameter();
                pars[6].ParameterName = "@deptId";
                pars[6].DbType = DbType.Int32;
                pars[6].Value = deptId;

                pars[7] = cmd.CreateParameter();
                pars[7].ParameterName = "@submitterId";
                pars[7].DbType = DbType.Int32;
                pars[7].Value = userId;

                pars[8] = cmd.CreateParameter();
                pars[8].ParameterName = "@CaseId";
                pars[8].DbType = DbType.Int32;
                pars[8].Value = CaseId;

                foreach (IDataParameter par in pars)
                {
                    cmd.Parameters.Add(par);
                }

                
                cmd.ExecuteNonQuery();

                //mSession.CreateSQLQuery(sql).ExecuteUpdate();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
